// Type of mixin
import { debounce } from 'lodash'
var observer = {
  callbacks: [],
  rss: function (type, fn) {
    this.callbacks.push({ type, fn })
  },
  issue: function (type, params) {
    this.callbacks.forEach(e => {
      if (e.type === type) {
        e.fn(params)
      }
    })
  }
}

export default {
  props: {
    dataForm: {
      type: Object,
      default: () => { }
    },
    scriptItem: {
      type: Object,
      default: () => { }
    },
    errorFieldsList: {
      type: Array,
      default: () => {
        return []
      }
    },
    isShowCaseSensitiveBase: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      selectDataObj: {},
      interfaceNoDataCount: 0,
      observer: observer
    }
  },
  created () {
    this.init()
  },
  methods: {
    init () {
      if (this.scriptItem.interfaceUrl) {
        this.getSelectDataLists(this.scriptItem)
      } else {
        this.selectDataObj[`${this.scriptItem.text}ListsOrigin`] = this.scriptItem.dataList
        this.selectDataObj = Object.assign({}, this.selectDataObj)
      }
      if (this.dataForm[this.scriptItem.text] === undefined || this.dataForm[this.scriptItem.text] === null) {
        // this.dataForm[this.scriptItem.text] = this.scriptItem.defaultValue || ''
        this.$set(this.dataForm, [this.scriptItem.text], this.scriptItem.defaultValue || '')
      }
    },
    getSelectDataLists (item) {
      const url = item.interfaceUrl
      let params = {
        tenantCode: this.$store.state.user.tenantCode
      }
      if (item.params && item.params['type'] && typeof item.params['type'] !== 'undefined') {
        params = {
          ...params,
          ...item.params
        }
      }
      this.$interface.clientReport.getBoardListForBotApi(url, params)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            this.resetDataLists(item)
            return this.$message.error(res.msg)
          }
          if (res.data) {
            this.selectDataObj[`${item.text}ListsOrigin`] = res.data
            this.selectDataObj = Object.assign({}, this.selectDataObj)
            if (res.data.length <= 0) {
              this.resetDataLists(item)
            }
          }
        }).catch(() => {
          this.resetDataLists(item)
        })
    },
    resetDataLists: debounce(function (item) {
      this.interfaceNoDataCount++
      if (this.interfaceNoDataCount > 5) {
        return false
      }
      this.getSelectDataLists(item)
    }, 2000),
    initDataForm () {
      try {
        if (this.errorFieldsList && this.errorFieldsList.length > 0) {
          this.dataForm.filters.rules.forEach(item => {
            if (item.rules) {
              item.rules.forEach(i => {
                let findIndex = this.errorFieldsList.findIndex(e => e.columnName === i.key && e.refLocation === 'widgetBuild_dataset_filter')
                if (findIndex > -1) {
                  i.fieldStatus = true
                }
              })
            } else {
              let findIndex = this.errorFieldsList.findIndex(e => e.columnName === item.key && e.refLocation === 'widgetBuild_dataset_filter')
              if (findIndex > -1) {
                item.fieldStatus = true
              }
            }
          })
        }
        this.$forceUpdate()
      } catch (error) {}
    }
  }
}
